import { HttpService } from "src/app/shared/service/http.service";
import { AppGlobalService } from "src/app/shared/service/app-global.service";
import { Component, OnInit, Input } from "@angular/core";
import { ModalController, AlertController } from "@ionic/angular";

@Component({
  selector: "app-recommend-modal",
  templateUrl: "./recommend-modal.component.html",
  styleUrls: ["./recommend-modal.component.scss"],
})
export class RecommendModalComponent implements OnInit {
  @Input() tagData: any;
  @Input() tipData: any;
  @Input() vipData: any;

  titleStyle = {};
  constructor(
    private httpService: HttpService,
    public modalController: ModalController,
    private appGlobalService: AppGlobalService,
    public alertController: AlertController
  ) { }

  ngOnInit() {

    if (this.tipData) {
      console.log(this.tipData);
    }

    // ? 蜂喔商户1.6.21 新增参数 联调
    if (this.tagData && this.tagData.data) {
      this.tagData.content = this.tagData.data;
    }
  }
  counselorCall(num) {
    this.callConsultantNow(num);
    this.modalController.dismiss({ dismissed: true, isBack: false });
  }

  async callConsultantNow(num) {
    const name = this.tipData ? this.tipData.downTag.name : "";
    const alert = await this.alertController.create({
      header: name,
      subHeader: "",
      message: num,
      buttons: [
        {
          text: "取消",
          role: "cancel",
          cssClass: "secondary",
          handler: () => {
            console.log("取消");
            //  产品确认不返回了
          },
        },
        {
          text: "拨打",
          handler: () => {

            const targetObj = JSON.parse(localStorage.getItem('targetObj'));
            if (targetObj && targetObj.bat && targetObj.bat.length > 0) {
              this.appGlobalService.popTarget();
              this.appGlobalService.goBack();
            }

            this.httpService.changeCallNumber(num);
          },
        },
      ],
    });
    await alert.present();
  }



  buttonClick(obj) {
    event.stopPropagation();
    try {
      const params = {
        content: obj.submit.content,
        id: obj.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      this.httpService.post("application/submit", params, res => {
        if (res.success) {
          this.appGlobalService.showToast(res.message, 1000, "middle");
          this.modalController.dismiss();
        } else {
          this.appGlobalService.showToast(res.message, 1000, "middle");
          this.modalController.dismiss();
        }
      });
    } catch (error) {
      console.log(JSON.stringify(error));
    }
  }
  onVipClick(btn) {
    if (btn.link) {
      this.appGlobalService.openPage(btn.link);
      this.modalController.dismiss({ dismissed: true });
    } else if (btn.submit) {
      btn.submit.sessionId = localStorage.getItem("sessionId");
      this.httpService.post("application/submit", btn.submit, (res) => {
        if (res.type && res.type === "page" && res.target) {
          this.appGlobalService.openPage({
            type: res.type,
            target: res.target,
            params: res.params ? res.params : {},
          });
        } else {
          this.appGlobalService.showToast(res.message, 1000, "middle");
        }
        this.modalController.dismiss({ dismissed: true });
      });
    }
  }
  closeClick() {
    this.modalController.dismiss();
  }
}
